<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <div class="operate">
        <el-button type="primary" @click="addRewardMapping">添加</el-button>
        <el-select v-model="searchParams.userId" placeholder="请选择学生" style="margin: 0 5px">
          <el-option v-for="(item, index) in users" :key="index" :label="item.nickName"
                     :value="item.userId"></el-option>
        </el-select>
        <el-select v-model="searchParams.rewardId" placeholder="请选择奖励" style="margin: 0 5px">
          <el-option v-for="(item, index) in rewards" :key="index" :label="item.rewardName"
                     :value="item.rewardId"></el-option>
        </el-select>
        <el-button type="primary" @click="getAllRewardMapping">搜索</el-button>
      </div>
    </div>
    <el-table
        :data="rewardMappings"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="user.nickName" label="学生名"></el-table-column>
      <el-table-column prop="reward.rewardName" label="奖励名"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="handleEdit(scope.$index, scope.row)">编辑奖励</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除奖励</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
    <reward-mapping-dialog :visible.sync="addDialog"  :users="users" :rewards="rewards" @addResult="getAllRewardMapping"></reward-mapping-dialog>
    <reward-mapping-update-dialog :visible.sync="updateDialog" :relationMapping="updateRewardMapping" :users="users" :rewards="rewards" @addResult="getAllRewardMapping"></reward-mapping-update-dialog>
  </div>
</template>

<script>
import {deleteRewardMapping, getAllReward, getAllRewardMapping} from "@/api/reward";
import {getAllUser} from "@/api/user";
import RewardMappingDialog from "@/components/dialog/RewardMappingDialog";
import RewardMappingUpdateDialog from "@/components/dialog/RewardMappingUpdateDialog";

export default {
  name: "rewardResult",
  components: {RewardMappingUpdateDialog, RewardMappingDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        rewardId:'',
        userId:''
      },
      rewardMappings:[],
      users:[],
      rewards:[],
      total:10,
      addDialog:false,
      updateRewardMapping:{},
      updateDialog:false
    }
  },
  methods:{
    getAllRewardMapping() {
      getAllRewardMapping(this.searchParams).then(res=>{
        this.rewardMappings=res.data.records;
      })
    },
    handleSizeChange(val){
      this.getAllRewardMapping();
    },
    handleCurrentChange(val){
      this.getAllRewardMapping();
    },
    handleEdit(index,row){
      this.updateRewardMapping=JSON.parse(JSON.stringify(row));
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteRewardMapping(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功退选")
          this.getAllRewardMapping();
        }
      })
    },
    addRewardMapping(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllRewardMapping();
    getAllUser().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].userType!=='0'){
            this.users.push({'userId':res.data[i].id,'nickName':res.data[i].nickName})
          }
        }
      }
    });
    getAllReward().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
          this.rewards.push({'rewardId':res.data[i].id,'rewardName':res.data[i].rewardName})
        }
      }
    })
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
    margin: 16px;
  }
}
</style>
